package com.sfac.javaSpringBoot.modules.account.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sfac.javaSpringBoot.modules.account.dao.UserDao;
import com.sfac.javaSpringBoot.modules.account.entity.User;
import com.sfac.javaSpringBoot.modules.account.service.UserService;
import com.sfac.javaSpringBoot.modules.common.entity.Result;
import com.sfac.javaSpringBoot.modules.common.entity.Search;
import com.sfac.javaSpringBoot.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    @Transactional                                                 //事务回滚
    public Result<User> insertUser(User user) {
        //1.设置当前时间
        user.setCreateDate(LocalDateTime.now());

        //1.1 密码加密
        user.setPassword(MD5Util.getMD5(user.getPassword()));

        //2.判断userName 或者 email 是否存在
        List<User> users = Optional
                .ofNullable(
                        userDao.getUsersByUserNameOrEmail(user.getUserName(),user.getEmail())
                ).orElse(Collections.emptyList());

        if (users.size()> 0 ){
            //如果存在则返回错误信息
            return new Result<>(
                    Result.ResultCode.FAILED.code, "Insert failed.");
        }

        //不存在进行插入操作，并返回正确信息
        userDao.insertUser(user);
        return new Result<>(
                Result.ResultCode.SUCCESS.code, "Insert success.", user);

    }

     @Override
     @Transactional
     public Result<User> updateUser(User user) {

     //判断userName 或者 email 是否存在
     List<User> users = Optional
             .ofNullable(
                        userDao.getUsersByUserNameOrEmail(user.getUserName(),user.getEmail())
             ).orElse(Collections.emptyList());

     if (users.size() > 0 && users.get(0).getId() != user.getId()){
        return new Result<>(
             Result.ResultCode.FAILED.code,"User name or email repeat.");
     }

     userDao.updateUser(user);
     return new Result<>(
             Result.ResultCode.SUCCESS.code,"Update success.",user);
     }
//----------------------------------------- 按id查 -------------------------------------
    @Override
    public User getUserById(int id) {
        return userDao.getUserById(id);
    }
    //不是增删改，不用回滚

//---------------------------------- 返回错误 ------------------------------------------
    @Override
    public Result<User> getUserByUserNameAndPassword(String userName, String password) {
        User user = userDao.getUserByUserNameAndPassword(
                userName, MD5Util.getMD5(password));
        if (user == null) {
            return new Result<>(
                    Result.ResultCode.FAILED.code, "用户名或密码错误.");
        }
        return new Result<>(
                Result.ResultCode.SUCCESS.code, "Login success.", user);
    }

    //按id删除
    @Override
    @Transactional
    public Result<Object> deleteUserById(int id) {
        userDao.deleteUserById(id);
        return new Result<>(
                Result.ResultCode.SUCCESS.code, "Delete success.");
    }
//-------------------------------------- 分页------------------------------------------
    @Override
    public PageInfo<User> getUsersBySearch(Search search) {
        PageHelper.startPage(search.getCurrentPage(), search.getPageSize());
        return new PageInfo<>(Optional
                .ofNullable(userDao.getUsersBySearch(search))
                .orElse(Collections.emptyList()));
    }
}